package com.quyeying.service.impl;

import com.quyeying.entity.TblProductSkuEntity;
import com.quyeying.mapper.TblProductSkuDao;
import com.quyeying.service.TblProductSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Map;

/**
* <p>
 * 商品规格属性表 服务实现类
 * </p>
*
* @author skx
* @since 2022-10-19
*/
@Service
public class TblProductSkuServiceImpl extends ServiceImpl<TblProductSkuDao, TblProductSkuEntity> implements TblProductSkuService {

    @Override
    public R findListByPage(Integer page, Integer pageCount){
        IPage<TblProductSkuEntity> wherePage = new Page<>(page, pageCount);
        TblProductSkuEntity where = new TblProductSkuEntity();
        IPage<TblProductSkuEntity> iPage = baseMapper.selectPage(wherePage, Wrappers.query(where));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(TblProductSkuEntity tblProductSkuEntity){
        baseMapper.insert(tblProductSkuEntity);
        return R.success();
    }

    @Override
    public R delete(Long id){
        baseMapper.deleteById(id);
        return R.success();
    }

    @Override
    public R updateData(TblProductSkuEntity tblProductSkuEntity){
        baseMapper.updateById(tblProductSkuEntity);
        return R.success();
    }

    @Override
    public R findById(Long id){
        TblProductSkuEntity tblProductSkuEntity = baseMapper.selectById(id);
        return R.success(tblProductSkuEntity);
    }

    @Override
    public Map<String, Object> findPriceRange(Long id) {
        Map<String, Object> priceRange = baseMapper.findPriceRange(id);
        double minPrice = 0;
        if (priceRange.get("minPrice").equals(priceRange.get("maxPrice"))){
            priceRange.put("minPrice",minPrice);
        }
        return priceRange;
    }
}
